package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.FinanceFindListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.ExtraValueContent;
import com.isunland.managesystem.entity.FinanceFind;
import com.isunland.managesystem.entity.FinanceFindOriginal;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/4/1 11:08
 */
public class FinanceFindListFragment extends BaseListFragment {

  private final int REQUEST_QUERY = 1;
  private ArrayList<FinanceFind> mList;
  private FinanceFindListAdapter<FinanceFind> mAdapter;
  private int mPage = 1;
  private String beginDate;
  private String endDate;
  private String discoverIntroductionCode;
  private String discoverIntroductionName;
  private String title;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.find);
    mList = new ArrayList<>();
    beginDate = MyDateUtil.parseDateToString(MyDateUtil.getBeforeMonth(3));
    endDate = MyDateUtil.parseDateToString(new Date());
    discoverIntroductionCode = "";
    discoverIntroductionName = "";
    title = "";
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mPullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_query, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      //查询
      case R.id.menu_item_query:
        ExtraValueContent queryConten = new ExtraValueContent();
        queryConten.setExtName1(beginDate);
        queryConten.setExtName2(endDate);
        queryConten.setExtName3(discoverIntroductionName);
        queryConten.setExtCode3(discoverIntroductionCode);
        queryConten.setExtName4(title);
        Intent query = new Intent(getActivity(), FinanceFindQueryActivity.class);
        query.putExtra(FinanceFindQueryFragment.EXTRA_CONTENT, queryConten);
        startActivityForResult(query, REQUEST_QUERY);
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public String getUrl() {
    return ApiConst.URL_FINANCE_FIND_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("memberCode", CurrentUser.newInstance(getActivity()).getMemberCode());
    params.put("page", mPage + "");
    params.put("pageSize", "20");
    params.put("beginregDate", beginDate);
    params.put("endregDate", endDate);
    params.put("discoverIntroduction", discoverIntroductionCode);
    params.put("title", title);

    return params;
  }

  @Override public void resolveResponse(String json) {
    FinanceFindOriginal result = new Gson().fromJson(json, FinanceFindOriginal.class);
    ArrayList<FinanceFind> rows = result.getRows();

    if ("1".equalsIgnoreCase(result.getResult()) && result.getRows() != null) {
      if (rows != null && rows.size() == 0) {
        ToastUtil.show(R.string.that_is_all);
      }

      //获取列表成功
      if (mPage == 1) {
        mList.clear();
      }
      mList.addAll(result.getRows());
      if (mAdapter == null) {
        mAdapter = new FinanceFindListAdapter(getActivity(), mList);
        setListAdapter(mAdapter);
        return;
      }
      mAdapter.notifyDataSetChanged();
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    FinanceFind content = mAdapter.getItem(position - 1);
    Intent i = new Intent(getActivity(), FinanceFindDetailActivity.class);
    i.putExtra(FinanceFindDetailFragment.EXTRA_CONTENT, content);
    startActivity(i);
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) {
    if (PullToRefreshBase.Mode.PULL_FROM_START == refreshView.getCurrentMode()) {
      mPage = 1;
    }
    if (PullToRefreshBase.Mode.PULL_FROM_END == refreshView.getCurrentMode()) {
      mPage++;
    }
    volleyPost();
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_QUERY) {
      ExtraValueContent queryConten =
          (ExtraValueContent) data.getSerializableExtra(FinanceFindQueryFragment.EXTRA_CONTENT);
      beginDate = queryConten.getExtName1();
      endDate = queryConten.getExtName2();
      discoverIntroductionCode = queryConten.getExtCode3();
      discoverIntroductionName = queryConten.getExtName3();
      title = queryConten.getExtName4();
      mPullRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
      mPage = 1;
      volleyPost();
      mPullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
    }
  }
}